using System;
using System.Text;
using System.Web.UI;
using EPiServer;
using EPiServer.Core;
using EPiSugar.Markup;
using EPiSugar.Navigation;

namespace EPiSugar.Content
{
    public class ContentGenerator
    {
        private readonly IPageSource pageSource;
        private readonly Page page;

        public ContentGenerator(Page page, IPageSource pageSource)
        {
            this.page = page;
            this.pageSource = pageSource;
        }

        public Tag A(PageData to)
        {
            return page.Tags().A(to.LinkURL, to.PageName);
        }

        public Tag A(string pageLinkProperty, string innerHtml)
        {
            if (pageSource.CurrentPage[pageLinkProperty] != null)
                return page.Tags().A(((PageReference)pageSource.CurrentPage[pageLinkProperty]).Page().LinkURL, innerHtml);
            return null;
        }

        public Tag Img(string propertyName)
        {
            return page.Tags().Img((string)pageSource.CurrentPage[propertyName]);
        }

        public int? Int(string propertyName)
        {
            if (pageSource.CurrentPage[propertyName] != null)
                return (int)pageSource.CurrentPage[propertyName];
            return null;
        }

        public string PageName(string pageLinkProperty)
        {
            if (pageSource.CurrentPage[pageLinkProperty] != null)
                return ((PageReference)pageSource.CurrentPage[pageLinkProperty]).Page().PageName;
            return null;
        }

        public object PropertyValue(string pageLinkProperty, string propertyName)
        {
            if (pageSource.CurrentPage[pageLinkProperty] != null)
                return ((PageReference)pageSource.CurrentPage[pageLinkProperty]).Page()[propertyName];
            return null;
        }

        public string PropertyString(string pageLinkProperty, string propertyName)
        {
            if (pageSource.CurrentPage[pageLinkProperty] != null)
                return (string)((PageReference)pageSource.CurrentPage[pageLinkProperty]).Page()[propertyName];
            return null;
        }

        public string GetPreviewText(PageData page, int PreviewTextLength)
        {
            return TextServices.GetPreviewText(page, PreviewTextLength);
        }

        /// <returns>Returns text for the specified properties on the given PageData</returns>
        public string GetText(PageData page, params string[] properties)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string property in properties)
            {
                if (page[property] != null)
                    sb.Append(page[property]).Append(" ");
            }

            if (sb.Length > 0)
                sb.Length--;

            return sb.ToString();
        }

        public PageReference GetReference(string pageLinkProperty)
        {
            return pageSource.CurrentPage[pageLinkProperty] as PageReference;
        }
    }
}